//入口
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import MintUI from 'mint-ui';
import 'mint-ui/lib/style.css'
Vue.use(MintUI)
import app from './app.vue'
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
import './lib/mui/fonts/mui-icons-extra.ttf'
import router from './router.js'
import VueResource from 'vue-resource'
Vue.use(VueResource)
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
import Vuex from 'vuex'
Vue.use(Vuex)

Vue.http.options.root = 'http://www.liulongbin.top:3005'
Vue.http.options.emulateJSON = true;
Vue.filter('dateFormatter', a => {
    var date = new Date(a);
    var y = date.getFullYear();
    var m = date.getMonth();
    var d = date.getDate();
    var h = date.getHours();
    var mm = date.getMinutes();
    var s = date.getSeconds();
    return `${y}-${m}-${d} ${h}:${mm}:${s}`;
})
var car = JSON.parse(localStorage.getItem('car')) || []
const store = new Vuex.Store({
    state: {
        car: car,
    },
    mutations: {
        addToShopCar(state, goodsInfo) {
            var flag = false
            state.car.some(item => {
                if (item.id === goodsInfo.id) {
                    item.selectCount = parseInt(item.selectCount) + parseInt(goodsInfo.selectCount)
                    flag = true
                    return true
                }
            })
            if (!flag) {
                state.car.push(goodsInfo)
            }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateSelectCount(state, goodsInfo) {
            state.car.forEach(item => {
                if (item.id == goodsInfo.goodsId) {
                    item.selectCount = goodsInfo.selectCount
                }
                return true
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        removeFromCar(state, goodsId) {
            state.car.forEach((item, i) => {
                if (item.id == goodsId) {
                    state.car.splice(i, 1)
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        changeSelected(state, goodsId) {
            state.car.forEach(item => {
                if (item.id == goodsId) {
                    item.selected = !item.selected
                }
            });
            console.log(state.car);
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters: {
        getCount: (state) => {
            var count = 0
            state.car.forEach(item => {
                count = count + parseInt(item.selectCount)
            });
            return count
        },
        getSelectCount(state) {
            var obj = {}
            state.car.forEach(item => {
                obj[item.id] = parseInt(item.selectCount)
            });
            return obj
        },
        getSelected(state) {
            var obj = {}
            state.car.forEach(item => {
                obj[item.id] = item.selected
            });
            return obj
        },
        getSelectCountAndAmount(state) {
            var obj = {}
            var count = 0
            var total = 0
            state.car.forEach(item => {
                if (item.selected) {
                    count += item.selectCount
                    total += item.selectCount * item.price
                }
            })
            obj.count = count
            obj.price = total
            return obj
        }
    }
})
var vm = new Vue({
    el: '#app',
    data: {

    },
    methods: {},
    render: h => h(app),
    router,
    store,
})